package com.xszx.dao.impl;

import com.xszx.bean.Manager;
import com.xszx.bean.User;
import com.xszx.dao.ManagerDao;
import com.xszx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public Manager login(String managerName, String managerPassword) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_manager where managername = ? and managerpassword = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, managerName);
            ps.setString(2, managerPassword);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                int role = resultSet.getInt("role");

                Manager manager = new Manager(id, managerName, managerPassword, role);
                return manager;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean addManager(Manager manager) {
        try {
            if (!existManagerName(manager.getManagerName())) {
                //管理名已存在
                return false;
            }
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_manager values(null, ?, '123456', ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, manager.getManagerName());
            ps.setInt(2, manager.getRole());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    //判断管理员名是否已经存在
    private boolean existManagerName(String managername) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_manager where managername = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, managername);

            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void updateManager(Manager manager) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_manager set managername = ?, managerpassword = ?, role = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, manager.getManagerName());
            ps.setString(2, manager.getManagerPassword());
            ps.setInt(3, manager.getRole());
            ps.setInt(4, manager.getId());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteManager(List<Integer> ids) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from t_manager where id in (?";
            for (int i = 1; i < ids.size(); i++) {
                sql += ", ?";
            }
            sql += ")";
            PreparedStatement ps = connection.prepareStatement(sql);
            for (int i = 0; i < ids.size(); i++) {
                ps.setInt(i+1, ids.get(i));
            }
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Manager getManagerById(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_manager where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);

            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String managerName = resultSet.getString("managername");
                String managerPassword = resultSet.getString("managerpassword");
                int role = resultSet.getInt("role");
                return new Manager(id, managerName, managerPassword, role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Manager> getAllManager() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_manager";
            PreparedStatement ps = connection.prepareStatement(sql);

            ResultSet resultSet = ps.executeQuery();
            List<Manager> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String managerName = resultSet.getString("managername");
                String managerPassword = resultSet.getString("managerpassword");
                int role = resultSet.getInt("role");
                list.add(new Manager(id, managerName, managerPassword, role));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
